package com.bankscene.screenfit.model;

import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;

import com.bankscene.screenfit.R;
import com.bankscene.screenfit.dict.ErrorDict;
import com.bankscene.screenfit.fragment.BTFragment;
import com.bankscene.screenfit.fragment.DES2EncryptFragment;
import com.bankscene.screenfit.fragment.FingerPrintFragment;
import com.bankscene.screenfit.fragment.IdAuthenticationFragment;
import com.bankscene.screenfit.fragment.MsCardFragment;
import com.bankscene.screenfit.fragment.NoneTouchIcCardFragment;
import com.bankscene.screenfit.fragment.PhotoCokKectFragment;
import com.bankscene.screenfit.fragment.PlainOutputFragment;
import com.bankscene.screenfit.fragment.PwdDownLoadFragment;
import com.bankscene.screenfit.fragment.RxJavaTestFragment;
import com.bankscene.screenfit.fragment.Secret2EncryptFragment;
import com.bankscene.screenfit.fragment.SerialNumbFragment;
import com.bankscene.screenfit.fragment.TouchIcCardFragment;
import com.bankscene.screenfit.fragment.VersionFragment;
import com.bankscene.screenfit.fragment.WritingPadFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;

public class Menus implements Parcelable {

    private Menus[] fragments;
    private int id;
    private String classSimpleName;
    private String title;
    private Integer icon;

    public Menus(int id, String title, Integer icon, String classSimpleName) {
        this.id = id;
        this.title = title;
        this.icon = icon;
        this.classSimpleName = classSimpleName;
    }

    public Menus(int id, String title, int icon, Menus[] fragments) {
        this.id = id;
        this.title = title;
        this.icon = icon;
        this.fragments = fragments;
    }

    public String getTitle() {
        return title;
    }

    public Integer getIcon() {
        return icon;
    }

    public int getId() {
        return id;
    }

    public String getClassName() {
        return classSimpleName;
    }

    public Menus[] getFragments() {
        return fragments;
    }

    public static Map<String, Menus> VALUES_MAP = new LinkedHashMap<>();

    public static final ArrayList<String> TITLES;
    public static final ArrayList<Menus> VALUES;

    static {
        VALUES_MAP.put("蓝牙连接", new Menus(R.id.bluetooth_navigation_menu_item, "蓝牙连接", R.drawable.menu_bluetooth, BTFragment.class.getName()));
        VALUES_MAP.put("身份认证", new Menus(R.id.idauthentication_navigation_menu_item, "身份认证", R.drawable.menu_idauthentication, IdAuthenticationFragment.class.getName()));
        VALUES_MAP.put("指纹验证", new Menus(R.id.fingerprint_navigation_menu_item, "指纹验证", R.drawable.menu_fingerprint, FingerPrintFragment.class.getName()));
        VALUES_MAP.put("接触式IC卡", new Menus(R.id.touchiccard_navigation_menu_item, "接触式IC卡", R.drawable.menu_touchiccard, TouchIcCardFragment.class.getName()));
        VALUES_MAP.put("非接触式IC卡", new Menus(R.id.nonetouchiccard_navigation_menu_item, "非接触式IC卡", R.drawable.menu_nonetouchiccard, NoneTouchIcCardFragment.class.getName()));
        VALUES_MAP.put("磁条卡", new Menus(R.id.mscard_navigation_menu_item, "磁条卡", R.drawable.menu_mscard, MsCardFragment.class.getName()));
        VALUES_MAP.put("影像采集", new Menus(R.id.photocokkect_navigation_menu_item, "影像采集", R.drawable.menu_photocokkect, PhotoCokKectFragment.class.getName()));

        Menus[] keyboardFragments = new Menus[]{
                new Menus(R.id.keyboard_navigation_menu_item, "国密加密", null, Secret2EncryptFragment.class.getName()),
                new Menus(R.id.keyboard_navigation_menu_item, "3DES加密", null, DES2EncryptFragment.class.getName()),
                new Menus(R.id.keyboard_navigation_menu_item, "原文输出", null, PlainOutputFragment.class.getName())
        };
        VALUES_MAP.put("密码键盘", new Menus(R.id.keyboard_navigation_menu_item, "密码键盘", R.drawable.menu_keyboard, keyboardFragments));

        VALUES_MAP.put("密钥下载", new Menus(R.id.pwddownload_navigation_menu_item, "密钥下载", R.drawable.menu_pwddownload, PwdDownLoadFragment.class.getName()));
        VALUES_MAP.put("手写板", new Menus(R.id.writingpad_navigation_menu_item, "手写板", R.drawable.menu_writingpad, WritingPadFragment.class.getName()));

        Menus[] systeminfoFragments = new Menus[]{
                new Menus(R.id.systeminfo_navigation_menu_item, "版本信息", null, VersionFragment.class.getName()),
                new Menus(R.id.systeminfo_navigation_menu_item, "序列号", null, SerialNumbFragment.class.getName()),
        };
        VALUES_MAP.put("系统信息", new Menus(R.id.systeminfo_navigation_menu_item, "系统信息", R.drawable.menu_systeminfo, systeminfoFragments));
        VALUES_MAP.put("退出", new Menus(R.id.logout_navigation_menu_item, "退出", R.drawable.menu_logout, null));
        VALUES_MAP.put("RxJava1", new Menus(R.id.logout_navigation_menu_item, "RxJava1", R.drawable.menu_logout, RxJavaTestFragment.class.getName()));

        TITLES = new ArrayList<>(Menus.VALUES_MAP.keySet());
        VALUES = new ArrayList<>(Menus.VALUES_MAP.values());
    }


    @Override
    public String toString() {
        return "Menus{" +
                "fragments=" + Arrays.toString(fragments) +
                ", id=" + id +
                ", classSimpleName='" + classSimpleName + '\'' +
                ", title='" + title + '\'' +
                ", icon=" + icon +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Menus menus = (Menus) o;

        if (id != menus.id) return false;
        // Probably incorrect - comparing Object[] arrays with Arrays.equals
        if (!Arrays.equals(fragments, menus.fragments)) return false;
        if (classSimpleName != null ? !classSimpleName.equals(menus.classSimpleName) : menus.classSimpleName != null)
            return false;
        if (title != null ? !title.equals(menus.title) : menus.title != null) return false;
        return icon != null ? icon.equals(menus.icon) : menus.icon == null;

    }

    @Override
    public int hashCode() {
        int result = Arrays.hashCode(fragments);
        result = 31 * result + id;
        result = 31 * result + (classSimpleName != null ? classSimpleName.hashCode() : 0);
        result = 31 * result + (title != null ? title.hashCode() : 0);
        result = 31 * result + (icon != null ? icon.hashCode() : 0);
        return result;
    }

    @CheckResult
    public static Menus getById(@NonNull Integer menuId) {
        checkNotNull(menuId, ErrorDict.ERROR_MENUSID_NOTBENULL);
        for (Menus menu : VALUES) {
            if (menu.getId() == menuId) {
                return menu;
            }
        }
        return null;
    }

    @CheckResult
    public static Menus getByPosition(@NonNull Integer position) {
        checkNotNull(position, ErrorDict.ERROR_MENUSPOSITION_NOTBENULL);
        return VALUES.get(position);
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelableArray(this.fragments, flags);
        dest.writeInt(this.id);
        dest.writeString(this.classSimpleName);
        dest.writeString(this.title);
        dest.writeValue(this.icon);
    }

    protected Menus(Parcel in) {
        Parcelable[] tempFs = in.readParcelableArray(Menus[].class.getClassLoader());
        if (tempFs != null) {
            this.fragments = Arrays.copyOf(tempFs, tempFs.length, Menus[].class);
        }
        this.id = in.readInt();
        this.classSimpleName = in.readString();
        this.title = in.readString();
        this.icon = (Integer) in.readValue(Integer.class.getClassLoader());
    }

    public static final Parcelable.Creator<Menus> CREATOR = new Parcelable.Creator<Menus>() {
        @Override
        public Menus createFromParcel(Parcel source) {
            return new Menus(source);
        }

        @Override
        public Menus[] newArray(int size) {
            return new Menus[size];
        }
    };
}
